import pyperclip


class BlockMover:
    """将指定行数的小单元块移动到文本末尾"""

    def __init__(self, n: int = 2):
        """
        n: 要移动到末尾的小单元行数
        """
        if n <= 0:
            raise ValueError("行数 n 必须为正整数")
        self.n = n

    def process_text(self, input_text: str) -> str:
        """处理文本，将长度为 n 的小单元移动到末尾"""
        blocks = []
        current_block = []

        for line in input_text.splitlines():
            if line.strip() == "":
                if current_block:
                    blocks.append(current_block)
                    current_block = []
            else:
                current_block.append(line)
        if current_block:
            blocks.append(current_block)

        # 分离长度为 n 的块
        n_line_blocks = [b for b in blocks if len(b) >= self.n]
        other_blocks = [b for b in blocks if len(b) < self.n]

        # 拼接最终顺序：先非 n 行，再 n 行
        final_blocks = other_blocks + n_line_blocks
        final_text = "\n\n".join("\n".join(b) for b in final_blocks)


        return final_text

    @staticmethod
    def read_input(end_marker="end") -> str:
        """从控制台读取多行文本"""
        print(f"请输入文本（输入 '{end_marker}' 表示结束）：")
        lines = []
        while True:
            line = input()
            if line.lower() == end_marker:
                break
            lines.append(line)
        return "\n".join(lines)


if __name__ == "__main__":
    try:
        n = int(input("请输入要移动到末尾的小单元行数 n："))
    except ValueError:
        print("输入错误，使用默认 n=2")
        n = 2

    input_text = BlockMover.read_input()
    mover = BlockMover(n=n)
    final_text = mover.process_text(input_text)

    print("\n✅ 已处理完成，结果已复制到剪贴板！")
    print(final_text)
